package buildcode;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;

import buildcode.build.BuildCommand;
import buildcode.database.DataBaseTable;
import buildcode.support.ContextIdConstant;
import buildcode.support.SmartBuildCommndBuilder;
import buildcode.util.DataBaseUtil;

public class EcejOrderCenterBuild {

    @Test
    public void test() {
        build("", "", new String[] { "db_2025_material_stats" }, new String[] {}, new String[] {});
    }

    public void build(String projectName, String module, String[] all, String[] daoOnly, String[] front) {

        List<DataBaseTable> res = DataBaseUtil.getTableInfo("jdbc:mysql://192.168.13.10/ecejdataboard?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "userdataboard", "cAI@!Zu+0ZOvZu&@",
                Stream.of(all, daoOnly, front).flatMap(Arrays::stream).toArray(String[]::new));
        String xmlMapperSrc = "D:\\git\\ecej-order-center\\ecej-order-base\\src\\main\\resources";
        String beanSrc = "D:\\git\\ecej-order-center\\ecej-order-model\\src\\main\\java";
        String mapperSrc = "D:\\git\\ecej-order-center\\ecej-order-base\\src\\main\\java";
        String enumSrc = "D:\\git\\ecej-order-center\\ecej-order-common\\src\\main\\java";
        String serviceSrc = "D:\\git\\ecej-order-center\\ecej-order-gateway-api\\src\\main\\java";
        String serviceImplSrc = "D:\\git\\ecej-order-center\\ecej-order-gateway\\src\\main\\java";
        String tplDir = "ecejordercenter";
        Context.setServletContextPath(projectName);
        Context.setAuthor("wangzhiyue");
        Context.setTablePrfix("db_2025_");
        for (DataBaseTable base : res) {
            if (ArrayUtils.contains(all, base.getTableName())) {
                BuildCommand entity = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_ENTITY, tplDir).setPackageName("com.ecej.model.po.databoard").setSrcLocation(beanSrc).setSuffix("Po")
                        .setTrimTablePrefix(true).build();
                BuildCommand bo = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_DTO, tplDir).setPackageName("com.ecej.model.bo.databoard").setSrcLocation(beanSrc).setSuffix("Bo")
                        .setTrimTablePrefix(true).build();
                BuildCommand enumEntity = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_ENUM, tplDir).setPackageName("com.ecej.order.common.enums").setSrcLocation(enumSrc)
                        .setTrimTablePrefix(true).build();
                BuildCommand javaMapper = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_JAVA_MAPPER, tplDir).setPackageName("com.ecej.order.base.dao.databoard").setSuffix("Dao")
                        .setSrcLocation(mapperSrc).setTrimTablePrefix(true).build();
                BuildCommand daoimpl = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_JAVA_MAPPER_IMPL, tplDir).setPackageName("com.ecej.order.base.dao.databoard.impl").setSuffix("DaoImpl")
                        .setSrcLocation(mapperSrc).setTrimTablePrefix(true).build();
                BuildCommand xmlMapper = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_XML_MAPPER, tplDir).setPackageName("mybatis.databoard").setSrcLocation(xmlMapperSrc)
                        .setExportFileType("xml").setTrimTablePrefix(true).build();
                BuildCommand service = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_SERVICE, tplDir).setPackageName("com.ecej.order.gateway.api.databoard")
                        .setSrcLocation(serviceSrc).setTrimTablePrefix(true).build();
                BuildCommand serviceImpl = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_SERVICE_IMPL, tplDir).setPackageName("com.ecej.order.gateway.api.databoard.impl")
                        .setSrcLocation(serviceImplSrc).setTrimTablePrefix(true).build();
                entity.setNext(bo).setNext(enumEntity).setNext(javaMapper).setNext(daoimpl).setNext(xmlMapper).setNext(service).setNext(serviceImpl);
                entity.execute(base);
            }
            if (ArrayUtils.contains(daoOnly, base.getTableName())) {

            }
        }
    }
}
